package com.godtree.util.bazhuayu;

import cn.hutool.core.lang.Tuple;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.godtree.model.BazhuanyuTask;
import com.google.common.collect.Maps;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.godtree.util.bazhuayu.TokenTools.getToken;

/**
 * @author Lisicheng
 * @date 2020-6-4
 */
public final class TaskTools {

  private static final String URL = "https://dataapi.bazhuayu.com/api/Task?taskGroupId={}";

  private static final String TASK_STATUS_URL = "https://dataapi.bazhuayu.com/api/task/GetTaskStatusByIdList";

  /**
   * 获取任务id集合
   *
   * @param taskGroupId 任务组id
   * @return 任务id集合
   */
  public static List<BazhuanyuTask> getTask(String taskGroupId) {
    JSONObject json = JSON.parseObject(
        HttpUtil.createGet(StrUtil.format(URL, taskGroupId)).header("Authorization", getToken())
            .execute().body());
    if (Constant.MSG.equals(json.getString(Constant.MSG_ERROR))) {
      return JSONUtil.parseArray(json.get("data")).stream().map(JSONUtil::parseObj)
          .map(item -> JSONUtil.toBean(item, BazhuanyuTask.class)).collect(Collectors.toList());
    }
    return Collections.emptyList();
  }

  /**
   * 获取任务状态
   *
   * @param taskIdList 任务id集合
   * @return 任务状态 key: 任务id value: 任务状态 运行中 = 0，已停止 = 1，已完成 = 2，待运行 = 3，未执行 = 5
   */
  public static Map<String, Integer> getTaskStatus(List<String> taskIdList) {
    // 等待10s再获取任务状态
    ThreadUtil.sleep(TimeUnit.SECONDS.toMillis(10));
    Map<String, Object> body = Maps.newHashMap();
    body.put("taskIdList", taskIdList);
    JSONObject json = JSONObject.parseObject(
        HttpUtil.createPost(TASK_STATUS_URL).header("Authorization", getToken())
            .body(JSONUtil.toJsonStr(body)).execute().body());
    if (Constant.MSG.equals(json.getString(Constant.MSG_ERROR))) {
      return JSONUtil.parseArray(json.get("data")).stream().map(JSONUtil::parseObj)
          .collect(Collectors.toMap(item -> item.getStr("taskId"), item -> item.getInt("status")));
    }
    return Collections.emptyMap();
  }

  /**
   * 启动任务
   *
   * @param taskId 任务id
   * @return 是否成功 true:成功 false:失败
   */
  public static Tuple startTask(String taskId) {
    String url = "https://dataapi.bazhuayu.com/api/task/StartTask?taskId={}";
    JSONObject json = JSONObject.parseObject(
        HttpUtil.createPost(StrUtil.format(url, taskId)).header("Authorization", getToken())
            .execute().body());
    if (Constant.MSG.equals(json.getString(Constant.MSG_ERROR))) {
      return new Tuple(true, json.getString(Constant.MSG_DESC));
    }
    return new Tuple(false, json.getString(Constant.MSG_DESC));
  }

  /**
   * 停止任务
   *
   * @param taskId 任务id
   * @return 是否成功 true:成功 false:失败
   */
  public static Tuple stopTask(String taskId) {
    String url = "https://dataapi.bazhuayu.com/api/task/StopTask?taskId={}";
    JSONObject json = JSONObject.parseObject(
        HttpUtil.createPost(StrUtil.format(url, taskId)).header("Authorization", getToken())
            .execute().body());
    if (Constant.MSG.equals(json.getString(Constant.MSG_ERROR))) {
      return new Tuple(true, json.getString(Constant.MSG_DESC));
    }
    return new Tuple(false, json.getString(Constant.MSG_DESC));
  }

}
